
//微信小程序根路径
const webURL = `/api`

// url为请求的路径，method为请求方式，data为要携带的参数，header为请求头，timeout为请求时间
const request = (o) => {
	const token = uni.getStorageSync('userInfo')?.token
	let options = {
		url: '',
		method: 'GET',
		data: {},
		header: {
			Authorization: token,
			'X-Device-ID': uni.getSystemInfoSync().deviceId,
			'X-Device-Mode': uni.getSystemInfoSync().deviceModel,
			'X-Device-Platform': uni.getSystemInfoSync().platform,
		},
		// 微信小程序请求时间一般设置为60000
		timeout: 60000
	}
	Object.assign(options, o)
	return new Promise((resolve, reject) => {
		uni.request({
			url: webURL + '/' + options.url,
			method: options.method,
			data: options.data,
			header: options.header,
			timeout: options.timeout,
			success(res) {
				if (res.data.code == 401) {
					uni.showLoading({
						title: "登录过期，请重新登陆",
					});
					uni.clearStorageSync();
					setTimeout(() => {
						uni.hideLoading();
						uni.redirectTo({ url: "/pages/information/index", })
					}, 1000);
				}
				resolve(res.data)
			},
			fail(err) {
				reject(err.data)
			}

		})
	})

}
export default request